package com.pig4cloud.pig.facility.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.facility.feign.entity.Facility;
import com.pig4cloud.pig.facility.service.FacilityService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;

@RestController
@RequiredArgsConstructor
@RequestMapping("/facility" )
@Tag(description = "facility" , name = "facility 表管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class FacilityController {
	@Autowired
	private final FacilityService facilityService;
	@Operation(summary = "新增facility 表" , description = "新增facility 表" )
	@SysLog("新增设施表" )
	@PostMapping("/addFacility")
	public R save(@Valid @RequestBody Facility facility) {
		Facility facility1 = facilityService.checkFacilityName(facility);
		if (facility1 != null) {
			return R.failed("该设施名称已存在");
		}
		return R.ok(facilityService.save(facility));
	}

	@DeleteMapping("/deleteFacility")
	@SysLog("删除设施表" )
	public R deleteFacility(@RequestParam Long[] ids) {
//		UpdateWrapper<Resident> updateWrapper = new UpdateWrapper<>();
//		updateWrapper.in("resident_id", Arrays.asList(ids));
//		updateWrapper.set("del_flag", "1");
//		return R.ok(residentService.update(updateWrapper));
		return R.ok(facilityService.removeByIds(Arrays.asList(ids)));
	}

	@PutMapping("/updateFacility")
	public R updateFacility(@Valid @RequestBody Facility facility) {
		Facility facility1 = facilityService.checkFacilityName(facility);
		if (facility1 != null && !facility1.getFacilityId().equals(facility.getFacilityId())) {
			return R.failed("该设施名称已存在");
		}
		return R.ok(facilityService.updateById(facility));
	}
	@GetMapping("/page")
	public R getUserPage(@ParameterObject Page page, @ParameterObject Facility facility) {
		QueryWrapper<Facility> wrapper = new QueryWrapper<>();
		wrapper.like(StrUtil.isNotEmpty(facility.getFacilityName()), "facility_name", facility.getFacilityName());
		wrapper.orderByDesc("create_time");
		return R.ok(facilityService.page(page, wrapper));
	}

	@GetMapping("/getFacilityById")
	public R getFacilityById(@RequestParam Long facilityId) {
		return R.ok(facilityService.getById(facilityId));
	}
}
